package com.example.recyclerview01.wrapper;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自定义，包裹效果的RV：
 * 每次使用的时候，不需要再包装一下。
 * 观察者模式：
 */
public class WrapRecyclerView extends RecyclerView {

    private WrapRecyclerAdapter mWrapRecyclerAdapter;
    // 带注册的观察者.
    private AdapterDataObserver mAdapterObserver = new AdapterDataObserver() {
        // 观察者的回调.
        @Override
        public void onChanged() {
            mWrapRecyclerAdapter.notifyDataSetChanged();
            Log.d("TAG", "onChanged ");
        }
        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            mWrapRecyclerAdapter.notifyItemRangeChanged(positionStart, itemCount);
        }
        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            mWrapRecyclerAdapter.notifyItemRangeChanged(positionStart, itemCount, payload);
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            mWrapRecyclerAdapter.notifyItemRangeInserted(positionStart, itemCount);
        }
        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            Log.d("TAG", "onItemRangeRemoved " + positionStart + " " + itemCount);
            mWrapRecyclerAdapter.notifyItemRangeRemoved(positionStart, itemCount);
        }
        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            mWrapRecyclerAdapter.notifyItemMoved(fromPosition, toPosition);
        }
    };

    public WrapRecyclerView(Context context) {
        this(context, null);
    }
    public WrapRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public WrapRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setAdapter(RecyclerView.Adapter adapter) {
        if (adapter instanceof WrapRecyclerAdapter) {
            mWrapRecyclerAdapter = (WrapRecyclerAdapter) adapter;
        } else {
            // 删除的bug: adapter中数据已经修改了。但是mWrapRecyclerAdapter中的数据没修改。
            // 所以需要关联---观察者.
            mWrapRecyclerAdapter = new WrapRecyclerAdapter(adapter);
            // 注册观察者
            adapter.registerAdapterDataObserver(mAdapterObserver);
        }
        super.setAdapter(mWrapRecyclerAdapter);
    }

    // 添加header、footer的方法
    public void addHeaderView(View view) {
        // 要先setAdapter，mWrapRecyclerAdapter才不为空.
        if (mWrapRecyclerAdapter != null) {
            mWrapRecyclerAdapter.addHeaderView(view);
        }
    }
    public void addFooterView(View view) {
        if (mWrapRecyclerAdapter != null) {
            mWrapRecyclerAdapter.addFooterView(view);
        }
    }
    public void removeHeaderView(View view) {
        if (mWrapRecyclerAdapter != null) {
            mWrapRecyclerAdapter.removeHeaderView(view);
        }
    }
    public void removeFooterView(View view) {
        if (mWrapRecyclerAdapter != null) {
            mWrapRecyclerAdapter.removeFooterView(view);
        }
    }
}
